package WarTug;

import java.util.ArrayList;

public class GameScreen {
	
	
	public static void switchToNameMenu(GraphicsManager gManager, UnitManager uManager, InputHandler iHandler, WarTug game){
		// This is the info for the name entry screen
		// Flush out the old screen components from GraphicsManager and InputHandler
		final GraphicsManager gMan = gManager;
		final UnitManager uMan = uManager;
		final InputHandler iHan = iHandler;
		final WarTug g = game;
		gManager.clearDrawableEntities();
		iHandler.clearButtonList();
		uManager.resetUnitLists();
		
		// Create the list of buttons that exist in the main menu screen
		ArrayList<Button> newButtons = new ArrayList<Button>();
		//Add the buttons here
		newButtons.add(new Button(5, 5, 10, 10, "Enter Your Name:", null)); // Button displaying "Enter Your Name" duh.
		final TextField textF = new TextField(100, 60, 200, 40, "", TextureHolder.textEntryBar);
		newButtons.add(textF); // Name Entry Field
		newButtons.add(new Button(225, 60, 40, 40, TextureHolder.tex/*mainMenuJoinButton*/, 
				new ButtonAction() {
					public void onClick() {
						g.name = textF.getText();
						GameScreen.switchToMainMenu(gMan, uMan, iHan, g);
					}
		
					public void hover() {}})); // Host New Game Button
		
		ArrayList<DrawableImage> newImages = new ArrayList<DrawableImage>();
		
		//Update the graphics manager, screen index, and input handler
		giveDrawablesToGraphicsHandler(newButtons, newImages, gManager);
		iHandler.setButtonList(newButtons);
		game.currentScreen = Screen.nameMenu;
	}
	
	
	
	
	public static void switchToMainMenu(GraphicsManager gManager, UnitManager uManager, InputHandler iHandler, WarTug game){
		// Flush out the old screen components from GraphicsManager and InputHandler
		// Matt Cotter--THIS IS WEIRD!!
		final GraphicsManager gMan = gManager;
		final UnitManager uMan = uManager;
		final InputHandler iHan = iHandler;
		final WarTug g = game;
		gManager.clearDrawableEntities();
		iHandler.clearButtonList();
		uManager.resetUnitLists();
		
		// Create the list of buttons that exist in the main menu screen
		ArrayList<Button> newButtons = new ArrayList<Button>();
		//Add the buttons here		
		newButtons.add(new Button(32, 16, 64, 32, "Host", TextureHolder.optionsWindowBackground/*mainMenuHostButton*/, 
				new ButtonAction() {
					public void onClick() {
						GameScreen.switchToGameMenu(gMan, uMan, iHan, g, true);
					}

					public void hover() {}})); // Host New Game Button
		
		newButtons.add(new Button(32, 48, 64, 32, "Join", TextureHolder.optionsWindowBackground/*mainMenuJoinButton*/, 
				new ButtonAction() {
					public void onClick() {
						GameScreen.switchToJoinMenu(gMan, uMan, iHan, g);
					}
		
					public void hover() {}})); // Join New Game Button
		
		ArrayList<DrawableImage> newImages = new ArrayList<DrawableImage>();
		//Add the images here
		//newImages.add(new DrawableImage(0,0,25,10,10, TextureHolder.tex/*mainMenuBackground*/));
		
		//Update the graphics manager, screen index, and input handler
		giveDrawablesToGraphicsHandler(newButtons, newImages, gManager);
		iHandler.setButtonList(newButtons);
		game.currentScreen = Screen.mainMenu;
	}
	
	public static void switchToJoinMenu(GraphicsManager gManager, UnitManager uManager, InputHandler iHandler, WarTug game){
		// Flush out the old screen components from GraphicsManager and InputHandler
		final GraphicsManager gMan = gManager;
		final UnitManager uMan = uManager;
		final InputHandler iHan = iHandler;
		final WarTug g = game;
		gManager.clearDrawableEntities();
		iHandler.clearButtonList();
		uManager.resetUnitLists();
		
		// Create the list of buttons that exist in the main menu screen
		ArrayList<Button> newButtons = new ArrayList<Button>();
		//Add the buttons here
		newButtons.add(new Button(5, 5, 10, 10, "Host IP:"));
		final TextField tF = new TextField(145, 60, 290, 40, "", TextureHolder.textEntryBar);
		newButtons.add(tF); // The IP Address TextField
		newButtons.add(new Button(315, 60, 40, 40, TextureHolder.tex/*TextureHolder.joinMenuJoinButton*/, 
				new ButtonAction() {
					public void onClick() {
						//TODO: CHECK TO SEE IF IT'S VALID!!!!!!  AND IF THERE IS A SERVER THERE TOO
						g.host = tF.getText();
						GameScreen.switchToGameMenu(gMan, uMan, iHan, g, false);
					}
					public void hover() {}})); // Join Game
		
		ArrayList<DrawableImage> newImages = new ArrayList<DrawableImage>();
		//Add the images here
		//newImages.add(new DrawableImage(0,0,25,600,400, TextureHolder.tex/*TextureHolder.joinMenuBackground*/));
		
		//Update the graphics manager, screen index, and input handler
		giveDrawablesToGraphicsHandler(newButtons, newImages, gManager);
		iHandler.setButtonList(newButtons);
		game.currentScreen = Screen.gameJoinMenu;
	}
	
	
	public static void switchToGameMenu(GraphicsManager gManager, UnitManager uManager, InputHandler iHandler, WarTug game, boolean createServer){
		// Flush out the old screen components from GraphicsManager and InputHandler
		final GraphicsManager gMan = gManager;
		final UnitManager uMan = uManager;
		final InputHandler iHan = iHandler;
		final WarTug g = game;
		gManager.clearDrawableEntities();
		iHandler.clearButtonList();
		uManager.resetUnitLists();
		
		if (createServer){
			game.playerNum = 0;
	    	game.network.setUpClientAndServer(true, "127.0.0.1", game);
	    } else {
			game.playerNum = 1;
	    	game.network.setUpClientAndServer(false, game.host, game);
	    }
		
		// Create gardens
		Garden gard0 = new Garden(0, 0);
		Garden gard1 = new Garden(608, 1);
		GardenButtons butts0 = new GardenButtons(gard0, gMan);
		GardenButtons butts1 = new GardenButtons(gard1, gMan);
		
		game.garden0 = new GardenEntity(gard0, butts0);
		game.garden1 = new GardenEntity(gard1, butts1);
		if (createServer)
			game.gOpts = new GardenOptionsWindow(gard0, gMan);
		else
			game.gOpts = new GardenOptionsWindow(gard1, gMan);
			
			
		// Create the list of buttons that exist in the main menu screen
		ArrayList<Button> newButtons = new ArrayList<Button>();
		
		
		
		ArrayList<DrawableImage> newImages = new ArrayList<DrawableImage>();
		//Add the images here
		//newImages.add(new DrawableImage(0,0,25,600,400, TextureHolder.tex/*TextureHolder.joinMenuBackground*/));
		
		//Update the graphics manager, screen index, and input handler
		giveDrawablesToGraphicsHandler(newButtons, newImages, gManager);
		iHandler.setButtonList(newButtons);
		game.currentScreen = Screen.inGame;
	}
	
	
	
	
	private static void giveDrawablesToGraphicsHandler(ArrayList<Button> buttons, ArrayList<DrawableImage> images, GraphicsManager gManager){
		// Add these buttons to GraphicsManager
		for (Button b: buttons){
			gManager.addDrawableEntity(b);
		}
		// Add these DrawableImages to GraphicsManager
		for (DrawableImage i: images){
			gManager.addDrawableEntity(i);
		}
	}
	
	
	
}
